
import express from "express";
import bodyParser from 'body-parser';
import cors from "cors"
import OracleDB from "./orcl.js";
import multer from "multer";
import { verifyToken } from "../util/tool.js";
import https from 'https';
import fs from "fs";


process.env.TZ = "Asia/Shanghai";
const port = 3002;
const app = express();
const api = express.Router();
const upload = multer({ dest: 'uploads/' });
const db = new OracleDB({
  user: 'scott',
  password: 'tiger',
//   connectString: '192.168.2.201:1521/orcl'
  connectString: 'localhost:1521/orcl'
});

await db.init();
// const options = {
//   key: fs.readFileSync('./server.key'),
//   cert: fs.readFileSync('./server.crt')
// };

app.use(bodyParser.json({ limit: '100mb' })); 
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' })); //解除包体大小限制
app.use(express.json()); //在所有未指定类型的请求体中使用JSON格式
app.use(cors()); // 解决跨域问题

app.listen(port, () => {
  console.log("Server is running on port " + port);
});
// https.createServer(options, app).listen(port, () => {
//   console.log('Server running at https://localhost:'+port);
// });



app.use((req, res, next) => {
      // 处理 OPTIONS 预检请求
    if (req.method === 'OPTIONS') {
        return res.sendStatus(200);
    }

    const whitelist = [
        '/api/flow/login',
        '/api/flow/getMenuByRole'
    ];

    if (whitelist.includes(req.path)) {
        return next();
    }

    verifyToken(req, res, next);
});

app.use('/api/flow', api);

app.use((err, req, res, next) => {
    console.error('全局异常:', err);
    res.status(500).json({
        code: 500,
        success: false,
        msg: err.message || err.toString(),
    });
});


export {api,upload,db}


